﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using System.ComponentModel.Design;

using ICSharpCode.SharpDevelop.Gui;
using ICSharpCode.Core.Presentation;

namespace AddInSample
{
    /// <summary>
    /// TestDockPan.xaml 的交互逻辑
    /// </summary>
    public partial class TestDockPan :UserControl, IViewContent
    {
        public TestDockPan()
        {
            InitializeComponent();
        }

        /// <summary>
        /// This is the UI element for the view.
        /// You can use both Windows.Forms and WPF controls.
        /// </summary>
        public  object Control
        {
            get
            {
                return this as Control;
            }
        }

        /// <summary>
        /// Gets the control which has focus initially.
        /// </summary>
        public virtual object InitiallyFocusedControl
        {
            get { return null; }
        }

        IWorkbenchWindow workbenchWindow;

        IWorkbenchWindow IViewContent.WorkbenchWindow
        {
            get { return workbenchWindow; }
            set
            {
                if (workbenchWindow != value)
                {
                    workbenchWindow = value;
                    OnWorkbenchWindowChanged();
                }
            }
        }

        public IWorkbenchWindow WorkbenchWindow
        {
            get { return workbenchWindow; }
        }

        protected virtual void OnWorkbenchWindowChanged()
        {
        }

        string tabPageText = "TabPageText";

        public event EventHandler TabPageTextChanged;

        /// <summary>
        /// Gets/Sets the title of the current tab page.
        /// This value will be passed through the string parser before being displayed.
        /// </summary>
        public string TabPageText
        {
            get { return tabPageText; }
            set
            {
                if (tabPageText != value)
                {
                    tabPageText = value;

                    if (TabPageTextChanged != null)
                    {
                        TabPageTextChanged(this, EventArgs.Empty);
                    }
                }
            }
        }

        #region TitleName
        public event EventHandler TitleNameChanged;

        void OnTitleNameChanged(EventArgs e)
        {
            if (TitleNameChanged != null)
            {
                TitleNameChanged(this, e);
            }
        }

        string titleName;
        LanguageDependentExtension titleNameLocalizeExtension;

        string IViewContent.TitleName
        {
            get
            {
                if (titleName != null)
                {
                    return titleName;
                }               
                else
                {
                    return "[Default Title]";
                }
            }
        }

        public string TitleName
        {
            get { return titleName; }
            set
            {
                if (titleNameLocalizeExtension != null)
                {
                    titleNameLocalizeExtension.PropertyChanged -= OnTitleNameLocalizationChanged;
                    titleNameLocalizeExtension = null;
                }
                if (titleName != value)
                {
                    titleName = value;
                    OnTitleNameChanged(EventArgs.Empty);
                }
            }
        }

        /// <summary>
        /// Sets a localized title that will update automatically when the language changes.
        /// </summary>
        /// <param name="text">The input to the string parser which will localize title.</param>
        protected void SetLocalizedTitle(string text)
        {
            titleNameLocalizeExtension = new StringParseExtension(text) { UsesAccessors = false };
            titleNameLocalizeExtension.PropertyChanged += OnTitleNameLocalizationChanged;
            OnTitleNameLocalizationChanged(null, null);
        }

        void OnTitleNameLocalizationChanged(object sender, EventArgs e)
        {
            string value = titleNameLocalizeExtension.Value;
            if (titleName != value)
            {
                titleName = value;
                OnTitleNameChanged(EventArgs.Empty);
            }
        }
        #endregion
        #region IsDirty
        bool IsDirtyInternal
        {
            get
            {
                //foreach (OpenedFile file in this.Files)
                //{
                //    if (file.IsDirty)
                //        return true;
                //}
                return false;
            }
        }

        bool isDirty;

        public virtual bool IsDirty
        {
            get { return isDirty; }
        }

        void OnIsDirtyChanged(object sender, EventArgs e)
        {
            bool newIsDirty = IsDirtyInternal;
            if (newIsDirty != isDirty)
            {
                isDirty = newIsDirty;
                RaiseIsDirtyChanged();
            }
        }

        /// <summary>
        /// Raise the IsDirtyChanged event. Call this method only if you have overridden the IsDirty property
        /// to implement your own handling of IsDirty.
        /// </summary>
        protected void RaiseIsDirtyChanged()
        {
            if (IsDirtyChanged != null)
                IsDirtyChanged(this, EventArgs.Empty);
        }

        public event EventHandler IsDirtyChanged;
        #endregion

        #region InfoTip
        public event EventHandler InfoTipChanged;

        void OnInfoTipChanged()
        {
            if (InfoTipChanged != null)
            {
                InfoTipChanged(this, EventArgs.Empty);
            }
        }

        string infoTip;
        LanguageDependentExtension infoTipLocalizeExtension;

        string IViewContent.InfoTip
        {
            get
            {
                if (infoTip != null)
                    return infoTip;              
                else
                    return null;
            }
        }

        public string InfoTip
        {
            get { return infoTip; }
            set
            {
                if (infoTipLocalizeExtension != null)
                {
                    infoTipLocalizeExtension.PropertyChanged -= OnInfoTipLocalizationChanged;
                    infoTipLocalizeExtension = null;
                }
                if (infoTip != value)
                {
                    infoTip = value;
                    OnInfoTipChanged();
                }
            }
        }

        /// <summary>
        /// Sets a localized info tip that will update automatically when the language changes.
        /// </summary>
        /// <param name="text">The input to the string parser which will localize info tip.</param>
        protected void SetLocalizedInfoTip(string text)
        {
            infoTipLocalizeExtension = new StringParseExtension(text) { UsesAccessors = false };
            infoTipLocalizeExtension.PropertyChanged += OnInfoTipLocalizationChanged;
            OnInfoTipLocalizationChanged(null, null);
        }

        void OnInfoTipLocalizationChanged(object sender, EventArgs e)
        {
            string value = infoTipLocalizeExtension.Value;
            if (infoTip != value)
            {
                infoTip = value;
                OnInfoTipChanged();
            }
        }
        #endregion

        #region IDisposable
        public event EventHandler Disposed;

        bool isDisposed;

        public bool IsDisposed
        {
            get { return isDisposed; }
        }

        public virtual void Dispose()
        {
            workbenchWindow = null;
            UnregisterOnActiveViewContentChanged();
            
            isDisposed = true;
            if (Disposed != null)
            {
                Disposed(this, EventArgs.Empty);
            }
        }
        #endregion

        	#region IsActiveViewContent
		EventHandler isActiveViewContentChanged;
		bool registeredOnViewContentChange;
		bool wasActiveViewContent;
		
		/// <summary>
		/// Gets if this view content is the active view content.
		/// </summary>
		protected bool IsActiveViewContent {
			get { return WorkbenchSingleton.Workbench != null && WorkbenchSingleton.Workbench.ActiveViewContent == this; }
		}
		
		/// <summary>
		/// Is raised when the value of the IsActiveViewContent property changes.
		/// </summary>
		protected event EventHandler IsActiveViewContentChanged {
			add {
				if (!registeredOnViewContentChange) {
					// register WorkbenchSingleton.Workbench.ActiveViewContentChanged only on demand
					wasActiveViewContent = IsActiveViewContent;
					// null check is required to support running in unit test mode
					if (WorkbenchSingleton.Workbench != null) {
						WorkbenchSingleton.Workbench.ActiveViewContentChanged += OnActiveViewContentChanged;
					}
					registeredOnViewContentChange = true;
				}
				isActiveViewContentChanged += value;
			}
			remove {
				isActiveViewContentChanged -= value;
			}
		}
		
		void UnregisterOnActiveViewContentChanged()
		{
			if (registeredOnViewContentChange) {
				// null check is required to support running in unit test mode
				if (WorkbenchSingleton.Workbench != null) {
					WorkbenchSingleton.Workbench.ActiveViewContentChanged -= OnActiveViewContentChanged;
				}
				registeredOnViewContentChange = false;
			}
		}
		
		void OnActiveViewContentChanged(object sender, EventArgs e)
		{
			bool isActiveViewContent = IsActiveViewContent;
			if (isActiveViewContent != wasActiveViewContent) {
				wasActiveViewContent = isActiveViewContent;
				if (isActiveViewContentChanged != null)
					isActiveViewContentChanged(this, e);
			}
		}
		#endregion

       /// <summary>
		/// Gets if the view content is read-only (can be saved only when choosing another file name).
		/// </summary>
		public virtual bool IsReadOnly {
			get { return false; }
		}
		
		/// <summary>
		/// Gets if the view content is view-only (cannot be saved at all).
		/// </summary>
		public virtual bool IsViewOnly {
			get { return false; }
		}

        #region IServiceProvider
        ServiceContainer services = new ServiceContainer();

        public object GetService(Type serviceType)
        {
            object obj = services.GetService(serviceType);
            if (obj == null)
            {
                if (serviceType.IsInstanceOfType(this))
                    return this;
            }
            return obj;
        }

        public ServiceContainer Services
        {
            get { return services; }
        }

        #endregion

        #region Secondary view content support
        sealed class SecondaryViewContentCollection : ICollection<IViewContent>
        {
            readonly AbstractViewContent parent;
            readonly List<IViewContent> list = new List<IViewContent>();

            public SecondaryViewContentCollection(AbstractViewContent parent)
            {
                this.parent = parent;
            }

            public int Count
            {
                get { return list.Count; }
            }

            public bool IsReadOnly
            {
                get { return false; }
            }

            public void Add(IViewContent item)
            {
                if (item == null)
                {
                    throw new ArgumentNullException("item");
                }
                if (item.WorkbenchWindow != null && item.WorkbenchWindow != parent.WorkbenchWindow)
                {
                    throw new ArgumentException("The view content already is displayed in another workbench window.");
                }
                list.Add(item);
                if (parent.WorkbenchWindow != null)
                {
                    parent.WorkbenchWindow.ViewContents.Add(item);
                }
            }

            public void Clear()
            {
                if (parent.WorkbenchWindow != null)
                {
                    foreach (IViewContent vc in list)
                    {
                        parent.WorkbenchWindow.ViewContents.Remove(vc);
                    }
                }
                list.Clear();
            }

            public bool Contains(IViewContent item)
            {
                return list.Contains(item);
            }

            public void CopyTo(IViewContent[] array, int arrayIndex)
            {
                list.CopyTo(array, arrayIndex);
            }

            public bool Remove(IViewContent item)
            {
                if (list.Remove(item))
                {
                    if (parent.WorkbenchWindow != null)
                    {
                        parent.WorkbenchWindow.ViewContents.Remove(item);
                    }
                    return true;
                }
                else
                {
                    return false;
                }
            }

            public IEnumerator<IViewContent> GetEnumerator()
            {
                return list.GetEnumerator();
            }

            System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
            {
                return list.GetEnumerator();
            }
        }

        readonly SecondaryViewContentCollection secondaryViewContentCollection;

        /// <summary>
        /// Gets the collection that stores the secondary view contents.
        /// </summary>
        public virtual ICollection<IViewContent> SecondaryViewContents
        {
            get
            {
                return secondaryViewContentCollection;
            }
        }

       
        #endregion
		
       
    }
}
